﻿using DevelopTool.Common;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.WiFiFixatorModels;
using Quartz;
using System;

namespace DevelopTool.Bll.WiFiFixatorBlls
{
    /// <summary>
    /// WiFi监控job
    /// </summary>
    public class WiFiFixatorJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            try
            {                
                //从配置文件中读取监控的WiFi ssid
                string wifiSSID = Setting.WiFiFixator.FixatorSSID;

                if (string.IsNullOrEmpty(wifiSSID))
                {
                    WiFiFixatorLogBll.AddLog(new LogModel(LogType.Warning, "未设定需要监控的WiFi"));
                    return;
                }

                //WiFi连接优先级
                int connectionPriority = Setting.WiFiFixator.WiFiConnectionPriority;

                WifiHelper wifiHelper = new WifiHelper();

                #region 本工具优先
                if (connectionPriority == 1)//本工具优先
                {
                    if (wifiHelper.WifiConnectedStatus(wifiSSID))//本机已连接WiFi
                    {
                        WiFiFixatorLogBll.AddLog(new LogModel(LogType.Info, $"WiFi监控规则[本工具优先]，已连接WiFi【{wifiSSID}】"));
                    }
                    else
                    {
                        WiFiFixatorLogBll.AddLog(wifiHelper.ConnectWiFi(wifiSSID));//连接监控的WiFi
                    }
                }
                #endregion 本工具优先

                #region 系统优先
                else if (connectionPriority == 2)
                {
                    if (wifiHelper.WifiConnectedStatus())//本机已连接WiFi
                    {
                        WiFiFixator wiFiFixator = wifiHelper.GetConnectedWiFi();

                        WiFiFixatorLogBll.AddLog(new LogModel(LogType.Info, $"WiFi监控规则[系统优先]，系统已连接WiFi【{wiFiFixator.Name}】"));
                    }
                    else//本机未连接WiFi时，那么就连接监控的WiFi
                    {
                        WiFiFixatorLogBll.AddLog(wifiHelper.ConnectWiFi(wifiSSID));
                    }
                }
                #endregion 系统优先

                else
                {
                    WiFiFixatorLogBll.AddLog(new LogModel(LogType.Warning, "未知监控优先级设定"));
                }                
            }
            catch (Exception ex)
            {
                WiFiFixatorLogBll.AddLog(new LogModel(LogType.Error, $"监控任务内部异常：{ex.Message}"));
            }

        }
    }
}
